﻿#include "twain_scan_window.h"
#include "ui_twain_scan_window.h"
#pragma execution_character_set("utf-8")；

twain_scan_window::twain_scan_window(QWidget *parent, Qt::WindowFlags f) :
    QWidget(parent),
    ui(new Ui::twain_scan_window)
{
    ui->setupUi(this);

    m_pTwain = new QTwain(this);
    is_install_twain = false;

    connect(ui->pushButton_start_scan, SIGNAL(clicked()), this, SLOT(onAcquireButton()));

    connect(m_pTwain, SIGNAL(already_get_sour_list()), this, SLOT(set_sour_list()));
}

twain_scan_window::~twain_scan_window()
{
    delete ui;
    delete m_pTwain;
}

void twain_scan_window::showEvent(QShowEvent* thisEvent)
{
    if(is_install_twain == false && m_pTwain)
    {
        m_pTwain->setParent(this);//设置接受消息的窗口句柄
        is_install_twain = true;
    }
}

void twain_scan_window::set_sour_list()
{
    if(m_pTwain->sourcelist.size() != 0)
    {
        for(int i = 0;i < m_pTwain->sourcelist.size();i++)
        {
            TW_IDENTITY temp_Source = m_pTwain->sourcelist.value(i);
            ui->comboBox_yiqi->addItem(temp_Source.ProductName,i);
        }
    }
    else
    {
        ui->comboBox_yiqi->addItem("没有可用的扫描仪");
    }

    //扫描类型
    ui->comboBox_type->addItem("单面扫描",0);
    ui->comboBox_type->addItem("双面扫描",1);

    //纸张大小
    ui->comboBox_papersize->addItem("A4",1);
    ui->comboBox_papersize->addItem("B5",2);
    ui->comboBox_papersize->addItem("USLETTER",3);
    ui->comboBox_papersize->addItem("USLEGAL",4);
    ui->comboBox_papersize->addItem("A5",5);
    ui->comboBox_papersize->addItem("B4",6);
    ui->comboBox_papersize->addItem("B6",7);
    ui->comboBox_papersize->addItem("B",8);
    ui->comboBox_papersize->addItem("USLEDGER",9);
    ui->comboBox_papersize->addItem("USEXECUTIVE",10);
    ui->comboBox_papersize->addItem("A3",11);
    ui->comboBox_papersize->addItem("B3",12);
    ui->comboBox_papersize->addItem("A6",13);
    ui->comboBox_papersize->addItem("C4",14);
    ui->comboBox_papersize->addItem("C5",15);
    ui->comboBox_papersize->addItem("C6",16);

    //颜色
    ui->comboBox_img_model->addItem("黑白",0);
    ui->comboBox_img_model->addItem("彩色",1);
    ui->comboBox_img_model->addItem("灰度图",2);

    //分辨率
    ui->comboBox_fenbianlv->addItem("150",150);
    ui->comboBox_fenbianlv->addItem("200",200);
    ui->comboBox_fenbianlv->addItem("300",300);
}

//接收消息
bool twain_scan_window::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    MSG* msg = reinterpret_cast<MSG*>(message);

    //转发消息
    if(m_pTwain->GetRC() <= 5 && m_pTwain->ProcessMessage(*msg))
    {
        return true;
    }
    return false;
}

void twain_scan_window::onAcquireButton()
{
    TW_IDENTITY Source = m_pTwain->sourcelist.value(ui->comboBox_yiqi->currentData().toInt());
    qDebug()<<Source.ProductName;
    qDebug()<<m_pTwain->DSOpen();
    m_pTwain->m_bSourceEnabled = true;
    m_pTwain->OpenSource(&Source);
    qDebug()<<m_pTwain->DSOpen();//数据源
    m_pTwain->m_bSourceEnabled = false;

    m_pTwain->type = ui->comboBox_type->currentData().toInt();
    m_pTwain->papersize = ui->comboBox_papersize->currentData().toInt();
    m_pTwain->img_model = ui->comboBox_img_model->currentData().toInt();
    m_pTwain->fenbianlv = ui->comboBox_fenbianlv->currentData().toInt();
    if (!m_pTwain->acquire())
    {
        qDebug()<<"未成功启动扫描程序";
    }
}
